//
//  MainMenuViewController.m
//  SAYAKA_Table
//
//  Created by 勘米良 on 13/07/20.
//  Copyright (c) 2013年 勘米良. All rights reserved.
//

#import "MainMenuViewController.h"
#import "ItemDetailViewController.h"
#import "OrderViewController.h"
#import "CategoryManager.h"
#import "AppDelegate.h"
@interface MainMenuViewController ()

@end

@implementation MainMenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
//    _categoryItems = [[NSMutableArray alloc] init];
    
//    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
//    NSArray *categorys = [CategoryManager allRecord:appDelegate.managedObjectContext];
    /////////////////////////////////////test data
    
//    for (int cnt = 0; cnt < 5; cnt++) {
//        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
//        Category *category = [categorys objectAtIndex:cnt];
//        [dic setObject:category forKey:@"category"];
//        
//        NSMutableArray *array = [[NSMutableArray alloc] init];
//        for (int itemCnt = 0; itemCnt < 7; itemCnt++) {
//            Item *item = [[Item alloc] init];
//            item.name = @"商品名";
//            item.price = (NSDecimalNumber*)[NSDecimalNumber numberWithInt:1000+itemCnt];
//            item.comment = @"うおおおおおおおおおお";
//            [array addObject:item];
//        }
//        [dic setObject:array forKey:@"item"];
//        
//        [_categoryItems addObject:dic];
//    }
    /////////////////////////////////////test data
    
    TileMenuView *view = [TileMenuView tileMenuView];
    //view.categoryItems = _categoryItems;
    view.delegate = self;
    [view update];

    self.view = view;
    [view.settingButton addTarget:self action:@selector(selectSetting:) forControlEvents:UIControlEventTouchUpInside];
    [view.orderButton addTarget:self action:@selector(selectOrder:) forControlEvents:UIControlEventTouchUpInside];
    
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)selectSetting:(id)sender
{
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MenuSetting" bundle:nil];
    UINavigationController* next = storyboard.instantiateInitialViewController;
    
    MenuSettingViewController *menuSttingView = (MenuSettingViewController*)next.topViewController;
    menuSttingView.caller = self;
    [self presentViewController:next animated:YES completion:nil];
}

- (IBAction)selectCategory:(id)sender {

    UIButton *button = (UIButton*)sender;
    switch (button.tag) {
            
        default:
            break;
    }
}

- (IBAction)selectOrder:(id)sender
{
    OrderViewController *vc = [[OrderViewController alloc] initWithNibName:@"OrderViewController" bundle:nil];
    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];
    [nvc setModalPresentationStyle:UIModalPresentationPageSheet];
    [self presentViewController:nvc animated:YES completion:nil];
}

- (void)tileMenuView:(TileMenuView *)tileMenuView didSelectItem:(Item *)item
{
    _selectItem = item;
    [self performSegueWithIdentifier:@"SuqueItemDetal" sender:self];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"SuqueItemDetal"]) {
        UINavigationController *naviController = (UINavigationController*)segue.destinationViewController;
        ItemDetailViewController *itemDetailView = (ItemDetailViewController*)naviController.topViewController;
        itemDetailView.item = _selectItem;
        _selectItem = nil;
    }
}

- (void)menuSettingViewControllerSettingEnded:(MenuSettingViewController *)controller
{
    [((TileMenuView*)self.view) reload];
}
@end
